//
//  ZZAdviceVC.m
//  新项目
//
//  Created by liGordon on 17/3/28.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZAdviceVC.h"
#import "PlaceholderTextView.h"
#import "ZZPassWordViewModel.h"
#define kTextBorderColor     RGBCOLOR(227,224,216)

#undef  RGBCOLOR
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@interface ZZAdviceVC ()<UITextViewDelegate>
@property (nonatomic, strong) PlaceholderTextView * textView;

@end

@implementation ZZAdviceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:229.0/255 green:229.0/255 blue:229.0/255 alpha:1.0f];
    
    [self.view addSubview:self.textView];
    

    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(clickForSend)];
    self.navigationItem.rightBarButtonItem=item;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
  //  [self.view addSubview:self.sendButton];
    
}

-(PlaceholderTextView *)textView{
    
    if (!_textView) {
        _textView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(0, 67, newwidth, newheight-67)];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:14.f];
        _textView.textColor = [UIColor blackColor];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.editable = YES;
        _textView.layer.cornerRadius = 4.0f;
        _textView.layer.borderColor = kTextBorderColor.CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.placeholderColor = RGBCOLOR(0x89, 0x89, 0x89);
        _textView.placeholder = @"您的建议是我们最大的财富...";
    }
    
    
    return _textView;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([@"\n" isEqualToString:text] == YES)
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}


- (UIColor *)colorWithRGBHex:(UInt32)hex
{
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex) & 0xFF;
    
    return [UIColor colorWithRed:r / 255.0f
                           green:g / 255.0f
                            blue:b / 255.0f
                           alpha:1.0f];
}

-(void)clickForSend
{
        NSLog(@"=======%@",self.textView.text);
    if(self.textView.text.length <= 0){
        
        [MBProgressHUD showError:@"请输入您的建议之后再进行提交"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
        
    }else{
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    NSLog(@"%@",loginDict);
    [[ZZPassWordViewModel shareViewModel]adviceFeedBackWithCompanyFID:loginDict[@"CompanyFID"] andCompanyID:loginDict[@"CompanyID"] andShopName:@"" andCompanyNmae:@"" andUserName:loginDict[@"LoginName"] andUserCode:@"" andPhone:@"" andContent:self.textView.text andSuccess:^(NSDictionary *resultObj) {
        if(resultObj){
            NSInteger theCode = [[resultObj objectForKey:@"code"] integerValue];
            if(theCode){
                NSDictionary *sucDict = [WHJSON objectFormJsonString:[resultObj objectForKey:@"resultJson"]];
                sucDict = [DMToolsObject replaceNullObjctWithDict:sucDict];
                [MBProgressHUD showSuccess:@"提交成功"];

            }else{
                NSString *messageStr = [resultObj objectForKey:@"message"];
                if(messageStr == nil || [messageStr isEqualToString:@""]){
                    messageStr = @"提交失败！";
                }
                [MBProgressHUD showError:messageStr];
            }
        }else{
   
            [MBProgressHUD showError:@"提交失败"];
            
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
        

    } andFailer:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"提交失败"];

    }];
    }
}

@end
